﻿
@{
    ViewData["Title"] = "WarehouseSetUpdateIndex";
    Layout = null;
}

<link href="~/libs/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />
<form class="layui-form" action="">
    <div class="layui-form-item">
        <label class="layui-form-label">仓库编码</label>
        <div class="layui-input-block">
            <input type="text" id="setCode" name="setCode" lay-verify="required" placeholder="请输入仓库编码" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">仓库名称</label>
        <div class="layui-input-block">
            <input type="text" id="setName" name="setName" lay-verify="required" placeholder="请输入仓库名称" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">位置</label>
        <div class="layui-input-block">
            <input type="text" id="setUnit" name="setUnit" lay-verify="required" placeholder="请输入仓库简称" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">面积</label>
        <div class="layui-input-block">
            <input type="number" lay-affix="number" placeholder="" name="setCapacity" id="setCapacity" value="0" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">负责人</label>
        <div class="layui-input-block">
            <input type="text" id="setPeople" name="setPeople" lay-verify="required" placeholder="请输入负责人" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">是否冻结</label>
        <div class="layui-input-block">
            <input type="radio" name="isNot" value="true" title="是" checked>
            <input type="radio" name="isNot" value="false" title="否">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <textarea name="result" id="result" placeholder="多行文本框" class="layui-textarea"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit lay-filter="demo1">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            <button type="reset" class="layui-btn layui-btn-primary" onclick="closeWorkstation()">返回</button>
        </div>
    </div>
</form>

<script src="~/libs/layui-v2.5.6/layui/layui.js"></script>
<script src="~/libs/jquery/jquery.js"></script>

<script>


    // 页面加载完成后执行的函数
    $(function () {
        mainUnitListOne();
        getMainUnitList();
    });

    // 获取主数据进行反填
    function mainUnitListOne() {
        console.log("获取客户数据");
        $.ajax({
            url: '/WarehouseSet/GetWarehouseSetById',
            type: 'get',
            data: {
                id: @ViewBag.Id
            },
            dataType: 'json',
            success: function (res) {
                console.log(res.data);
                $("#setCode").val(res.data.setCode);
                $("#setName").val(res.data.setName);
                $("#setUnit").val(res.data.setUnit);
                $("#setCapacity").val(res.data.setCapacity);
                $("#setPeople").val(res.data.setPeople);
                $("#result").val(res.data.result);
                // 设置 radio 选中状态
                $('input[name="isNot"][value="' + res.data.isNot + '"]').prop('checked', true);
               
                layui.form.render("select");
                layui.form.render("radio");
            }
        });
    }


    layui.use(['form', 'layer'], function () {
        var form = layui.form;
        var layer = layui.layer;

        // 监听是否启用的选择
        form.on('radio(isNot)', function (data) {
            var value = data.value; // 获取选中的值
            if (value === 'true') {
           
                $('#mainUnitFields').hide();
            } else {
        
                $('#mainUnitFields').show();
            }
        });

        // 提交事件
        form.on('submit(demo1)', function (data) {
      

            var field = data.field;
            field.id = @ViewBag.Id;
            field.isNot = field.isNot === 'true'; // 将字符串转换为布尔值
            $.ajax({
                url: '/WarehouseSet/UpdateWarehouseSet',
                type: 'post',
                data: field,
                dataType: 'json',
                success: function (res) {
                    if (res.code == true) {
                        layer.alert('修改成功！', function () {
                            closeWorkstation();
                        });
                    } else if (res.code == false) {
                        layer.alert('修改失败！,编号或名称已存在！');
                    }
                }
            });
            
            return false; // 阻止默认 form 跳转

        });
    });

    // 关闭弹框
    function closeWorkstation() {
        var index = parent.layer.getFrameIndex(window.name); // 先得到当前的索引
        parent.layer.close(index); // 再执行关闭
    }
</script>

